package com.zlm.app;

import com.zlm.bean.AdClickEvent;
import com.zlm.bean.AdCountViewByProvince;
import com.zlm.common.BlacklistWarning;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.timestamps.AscendingTimestampExtractor;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.net.URL;

/**
 * Author: Harbour
 * Date: 2021-05-17 11:21
 * Desc: 分析广告行为APP
 */
public class AdBlacklistApp {
    public static void main(String[] args) throws Exception {
        // step 1 获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);

        // step 2 获取数据源
        URL resource = AdBlacklistApp.class.getResource("/AdClickLog.csv");
        DataStream<String> inputDataStream = env.readTextFile(resource.getPath());

        // step 3 转换数据类型
        DataStream<AdClickEvent> adClickEventDataStream = inputDataStream
                .map(new MapFunction<String, AdClickEvent>() {
                    @Override
                    public AdClickEvent map(String value) throws Exception {
                        String[] fields = value.split(",");

                        // Long userId, Long adId, String province, String city, Long timestamp
                        return new AdClickEvent(
                                new Long(fields[0]),
                                new Long(fields[1]),
                                fields[2],
                                fields[3],
                                new Long(fields[4])
                        );
                    }
                })
                .assignTimestampsAndWatermarks(new AscendingTimestampExtractor<AdClickEvent>() {
                    @Override
                    public long extractAscendingTimestamp(AdClickEvent element) {
                        return element.getTimestamp() * 1000L;
                    }
                });


        // step 4 黑名单处理
        SingleOutputStreamOperator<AdClickEvent> adClickEventWithBlacklistDataStream = adClickEventDataStream
                .keyBy("userId", "adId")
                .process(new MyBlacklistProcessFunction(100));


        // step 5 开窗统计
        adClickEventWithBlacklistDataStream
                .keyBy(AdClickEvent::getProvince)
                .timeWindow(Time.hours(1), Time.minutes(5))
                .aggregate(new MyAggFunction(), new MyFullWindowFunction())
//                .keyBy(AdCountViewByProvince::getWindowEnd)
//                .process(new MyKeyedProcessFunction())
                .print();

        // step 6 输出黑名单统计信息
        adClickEventWithBlacklistDataStream
                .getSideOutput(new OutputTag<BlacklistWarning>("blacklist"){})
                .print("black-list");

        env.execute();
    }

    private static class MyBlacklistProcessFunction extends KeyedProcessFunction<Tuple, AdClickEvent, AdClickEvent> {

        private final int upperBound; // 超过点击次数，则拉入黑名单

        // 用户点击次数与当前是否已经被加入黑名单
        ValueState<Integer> clickCountState;
        ValueState<Boolean> isSendState;

        public MyBlacklistProcessFunction(int upperBound) {
            this.upperBound = upperBound;
        }

        @Override
        public void open(Configuration parameters) throws Exception {
            clickCountState = getRuntimeContext().getState(new ValueStateDescriptor<Integer>("adClickCount", Integer.class, 0));
            isSendState = getRuntimeContext().getState(new ValueStateDescriptor<Boolean>("isSend", Boolean.class, false));
        }

        @Override
        public void processElement(AdClickEvent value, Context ctx, Collector<AdClickEvent> out) throws Exception {
            Integer clickCount = clickCountState.value();
            Boolean isSend = isSendState.value();

            //如果第一次处理，注册一个定时器
            if (clickCount == 0) {
                long ts = (ctx.timerService().currentProcessingTime() / (24 * 60 * 60 * 1000) + 1) * (24 * 60 * 60 * 1000);
                ctx.timerService().registerEventTimeTimer(ts);
            }

            // 超过点击次数，不收集该数据
            if (clickCount > upperBound) {
                // 尚未加入过黑名单的，将其加入黑名单
                if (!isSend) {
                    ctx.output(
                            new OutputTag<BlacklistWarning>("blacklist"){},
                            new BlacklistWarning(
                                    value.getUserId().toString(),
                                    value.getAdId().toString(),
                                    "广告点击次数为" + clickCount + "次，过于频繁，已加入黑名单"
                            )
                    );
                    isSendState.update(true);
                }
                return;
            }
            clickCountState.update(clickCount + 1);
            out.collect(value);
        }

        @Override
        public void onTimer(long timestamp, OnTimerContext ctx, Collector<AdClickEvent> out) throws Exception {
            // 触发定时器，解除黑名单状态
            clickCountState.clear();
            isSendState.clear();
        }
    }

    private static class MyFullWindowFunction implements WindowFunction<Long, AdCountViewByProvince, String, TimeWindow> {

        @Override
        public void apply(String s, TimeWindow window, Iterable<Long> input, Collector<AdCountViewByProvince> out) throws Exception {
            out.collect(
                    new AdCountViewByProvince(s, String.valueOf(window.getEnd()), input.iterator().next())
            );
        }
    }

    private static class MyAggFunction implements AggregateFunction<AdClickEvent, Long, Long> {

        @Override
        public Long createAccumulator() {
            return 0L;
        }

        @Override
        public Long add(AdClickEvent value, Long accumulator) {
            return accumulator + 1;
        }

        @Override
        public Long getResult(Long accumulator) {
            return accumulator;
        }

        @Override
        public Long merge(Long a, Long b) {
            return a + b;
        }
    }

    private static class MyKeyedProcessFunction extends KeyedProcessFunction<String, AdCountViewByProvince, String> {

        @Override
        public void processElement(AdCountViewByProvince value, Context ctx, Collector<String> out) throws Exception {

            out.collect(">>>>" + ctx.getCurrentKey() + ">>>" + value);
        }
    }
}
